package com.xmtlxx.common.utils.file;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.io.*;

/**
 * @author RonyChan
 * @since 2022/4/10 17:59
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LocalJsonUtil {

    //读取JSON文件To字符串
    public static String ReadFile(String path) throws IOException{
        StringBuffer sb = new StringBuffer();
        BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }

    //写文件
    public static void WriteFile(String path, String content) throws IOException{
        Clear(path);//清空文本内容
        File f = new File(path);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
        bw.write(content);
        bw.close();
    }

    public static void Clear(String path) throws IOException{
        File f = new File(path);
        FileWriter fw =  new FileWriter(f);
        fw.write("");
        fw.close();
    }

}
